package cn.sx10.dao.impl;

import cn.sx10.dao.BaseDao;
import cn.sx10.dao.StudentDao;
import cn.sx10.model.Student;


import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class studentDaoimpl<studentno> extends BaseDao implements StudentDao {

    @Override
    public List<Student> findAll(){
        //SQL语句
        String sql = "SELECT * FROM student";

        //执行SQL
        return super.queryToList(Student.class,sql);
    }

    @Override
    public List<Student> singleDemand(int studentno) {
        String sql = "SELECT * FROM student where studentno=?";

        //执行SQL
        return super.queryToList(Student.class,sql,studentno);
    }

    @Override
    public int findAdd(Student student) {
        String sql = "INSERT INTO student (studentNo,studentName,loginPwd,sex,gradeId,phone,address,borndate,email,identityCard) VALUES(?,?,?,?,?,?,?,?,?,?)";

        //执行SQL
        int rowcount= super.insertUpdateDelete(sql,
                                            student.getStudentNo(),
                                            student.getStudentName(),
                                            student.getLoginPwd(),
                                            student.getSex(),
                                            student.getGradeId(),
                                            student.getPhone(),
                                            student.getAddress(),
                                            student.getBornDate(),
                                            student.getEmail(),
                                            student.getIdentityCard()
        );
        return rowcount;
    }

    @Override
    public int delete(int studentno) {
        String sql = " DELETE FROM student WHERE studentno = ?";
        return super.insertUpdateDelete(sql,studentno);
    }

    @Override
    public Student modifyview(String studentno) {
        String sql = "select * from student where studentno = ?";
        return super.queryToSingle(Student.class,sql,studentno);
    }

    @Override
    public int modify(Student student) {
        String sql="UPDATE student \n" +
                "SET loginpwd=?,\n" +
                "    studentname=?,\n" +
                "    sex=?,\n" +
                "    gradeid=?,\n" +
                "    phone=?,\n" +
                "    address=?,\n" +
                "    borndate=?,\n" +
                "    email=?,\n" +
                "    identitycard=?\n" +
                " WHERE studentno=?";
        int rowcount = super.insertUpdateDelete(sql,
                student.getLoginPwd(),
                student.getStudentName(),
                student.getSex(),
                student.getGradeId(),
                student.getPhone(),
                student.getAddress(),
                student.getBornDate(),
                student.getEmail(),
                student.getIdentityCard(),
                student.getStudentNo()
                );
        return rowcount;
    }

    @Override
    public boolean queryNum(String studentNo) {
        String sql = "select * from student where studentno=?";

        return super.queryToSingle(Student.class,sql,studentNo)==null?true:false;
    }

    @Override
    public List<Student> search(Student student) {
        String sql = "select * from student where 1=1";
        List<Object> datalist = new ArrayList<>();
        if (student.getStudentNo() !=0){
            sql += " and studentno =?";
            datalist.add(student.getStudentNo());
        }
        if (student.getStudentName() !=null && student.getStudentName()!="" && student.getStudentNo() != 0 ){
            sql += "|| studentname like ?";
            datalist.add("%"+student.getStudentName()+"%");
        }
        if (student.getStudentName() !=null && student.getStudentName()!="" && student.getStudentNo() == 0){
            sql += " and studentname like ?";
            datalist.add("%"+student.getStudentName()+"%");
        }
        Object[] arrays = datalist.toArray();
        List<Student> result = super.queryToList(Student.class,sql,arrays);
        return result;
    }
}
